package cn.wen.yinxiu.works.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.common.toolkit.DateUtils;
import cn.wen.yinxiu.common.toolkit.StringUtils;
import cn.wen.yinxiu.works.dao.WorksDao;
import cn.wen.yinxiu.works.entity.DiagnosisEntity;
import cn.wen.yinxiu.works.entity.WorksDataEntity;
import cn.wen.yinxiu.works.entity.WorksEntity;
import cn.wen.yinxiu.works.feign.UserFeignService;
import cn.wen.yinxiu.works.service.DiagnosisService;
import cn.wen.yinxiu.works.service.WorksDataService;
import cn.wen.yinxiu.works.service.WorksService;
import cn.wen.yinxiu.works.to.UserDataInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.wen.yinxiu.works.dao.WeekReportDao;
import cn.wen.yinxiu.works.entity.WeekReportEntity;
import cn.wen.yinxiu.works.service.WeekReportService;

import java.util.*;


@Service("weekReportService")
public class WeekReportServiceImpl extends ServiceImpl<WeekReportDao, WeekReportEntity> implements WeekReportService {

    @Autowired
    private WeekReportDao weekReportDao;

    @Autowired
    private WorksDao worksDao;

    @Autowired
    private DiagnosisService diagnosisService;

    @Override
    public WeekReportEntity queryWeekReportInfo(Long userId, String preWeekStartTime) {
        QueryWrapper<WeekReportEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("start_time", preWeekStartTime);
        return weekReportDao.selectOne(queryWrapper);
    }

    @Override
    public boolean createWeekReportInfo(Long userId) {
        // 1.获取周期
        String lastWeekStartTime = DateUtils.getLastWeekStartTimeStr();
        String currentWeekStartTime = DateUtils.getWeekStartTimeStr();
        // 上周的最后一天
        String lastWeekEndTime = DateUtils.getLastWeekEndTimeStr();
        // 2.调用接口 获取上周七天的数据
        DiagnosisEntity diagnosisEntity = diagnosisService.getWorksDiagnosisByTime(currentWeekStartTime, userId);
        // 3.计算对应的数据（计算上周增加的数据 包括：用户作品量、粉丝量、点赞量、收藏量、播放量、评论量、
        WeekReportEntity target = new WeekReportEntity();
        target.setIncrPlayNum(diagnosisEntity.getPlayNum());
        target.setWorksNum(diagnosisEntity.getWorksNum());
        target.setIncrLikeNum(diagnosisEntity.getLikeNum());
        target.setIncrCollectNum(diagnosisEntity.getCollectNum());
        target.setIncrFansNum(diagnosisEntity.getFansNum());
        target.setPlayExceed(diagnosisEntity.getPlayExceed());
        target.setWorksNumExceed(diagnosisEntity.getWorksExceed());
        target.setUserId(userId);
        target.setStartTime(DateUtils.getDateFromStr(lastWeekStartTime));
        target.setEndTime(DateUtils.getDateFromStr(lastWeekEndTime));
        target.setCreateTime(new Date());
        // 4.获取本周/全部播放量 AND 点赞量最高的
        WorksEntity weekWorksEntity = getWorksBestInfoByTime(userId, lastWeekStartTime, lastWeekEndTime);
        WorksEntity allWorksEntity = getWorksBestInfoByTime(userId, null, null);
        target.setWeekWorksId(weekWorksEntity.getId());
        target.setAllWorksId(allWorksEntity.getId());
        // 5.插入数据
        int res = weekReportDao.insert(target);
        return res > 0;
    }

    /**
     * 通过时间周期获取最佳作品
     *
     * @param lastWeekStartTime
     * @param lastWeekEndTime
     * @return
     */
    WorksEntity getWorksBestInfoByTime(Long userId, String lastWeekStartTime, String lastWeekEndTime) {
        QueryWrapper<WorksEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (StringUtils.isNotEmpty(lastWeekEndTime)) {
            queryWrapper.ge("create_time", lastWeekStartTime);
            queryWrapper.lt("create_time", lastWeekEndTime);
        }
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        queryWrapper.orderByDesc("play_counts").orderByDesc("like_counts");
        queryWrapper.last("LIMIT 1");
        return worksDao.selectOne(queryWrapper);
    }
}